package com.webdesign.util;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

public class FileToolsUtil {
	
	/**
	 * 将content写入file，默认是utf-8编码，覆盖写
	 * @param path    路径
	 * @param name    名称，带后缀
	 * @param content 内容
	 * @return 1        正常
	 *         0   UnsupportedEncodingException
	 *        -1   FileNotFoundException
	 *        -2   IOException
	 */
	public static int string2file(String path, String name, String content) {
		return string2file(path, name, content, "utf-8");
	}
	
	public static int string2file(String path, String name, String content, String charsetName) {
		return string2file(path, name, content, charsetName, false);
	}
	
	public static int string2file(String path, String name, String content, String charsetName, boolean append) {
		int flag = 1;
		String filename = path + name;
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, append), charsetName));
			bw.write(content);
			bw.close();
			flag = 1;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			flag = 0;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			flag = -1;
		} catch (IOException e) {
			e.printStackTrace();
			flag = -2;
		}
		return flag;
	}
	
	public static String file2string(File file) {
		return file2string(file, "gbk");
	}
	
	public static String file2string(File file, String charset) {
		String output = "";
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), charset));
			StringBuffer buffer = new StringBuffer();
			int oneChar;
			while ((oneChar = input.read()) != -1) {
				buffer.append((char)oneChar);
			}
			output = buffer.toString();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return output;
	}
	
	public static String file2string(String path, String charset) {
		String output = "";
		File file = new File(path);
		
		if (file.exists() == false) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), charset));
			StringBuffer buffer = new StringBuffer();
			int oneChar;

			while ((oneChar = input.read()) != -1) {
				buffer.append((char)oneChar);
			}
			output = buffer.toString();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return output;
	}
	
	public static int moveFile(String _srcFile, String _dstFile, String srcCharset, String dstCharset) {
		int flag = 1;
		File srcFile = new File(_srcFile);
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					new FileInputStream(srcFile), srcCharset));
			BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(_dstFile, true), dstCharset));
			String oneLine;
			while ((oneLine = input.readLine()) != null) {
				output.write(oneLine + "\n");
			}
			input.close();
			output.close();

		} catch (IOException e) {
			e.printStackTrace();
			flag = 0;
		}
		
		srcFile.delete();
		
		return flag;
	}
	
	/**
	 * 将str中的非法文件名字符替换成空格
	 * @param str
	 * @return
	 */
	public static String toValidfilename(String str) {
		Pattern filePattern = Pattern.compile("[\\\\/:*?\"<>|]");
		String ret = "";
		if (str == null) {
			System.err.println("filename is null...");
			return "";
		} else {
			ret = filePattern.matcher(str).replaceAll(" ");
		}
		return ret;
	}
}
